1 Imports System.Data.SqlClient
2 Public Class frmRegistration
3     Dim st1 As String
4     Sub Reset()
5         txtContactNo.Text =
""
6         txtEmailID.Text =
""
7         txtName.Text =
""
8         txtPassword.Text =
""
9         txtUserID.Text =
""
10         cmbUserType.SelectedIndex = -
1
11         chkActive.Checked = True
12         txtUserID.Focus()
13         btnSave.Enabled = True
14         btnUpdate.Enabled = False
15         btnDelete.Enabled = False
16     End Sub
17     Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
18         Me.Close()
19     End Sub
20
21     Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
22         If txtUserID.Text =
"" Then
23             MessageBox.Show(
"Please enter user id", "", MessageBoxButtons.OK, MessageBoxIcon.Warning)
24             txtUserID.Focus()
25             Return
26         End If
27         If cmbUserType.Text =
"" Then
28             MessageBox.Show(
"Please select user type", "", MessageBoxButtons.OK, MessageBoxIcon.Warning)
29             cmbUserType.Focus()
30             Return
31         End If
32         If txtPassword.Text =
"" Then
33             MessageBox.Show(
"Please enter password", "", MessageBoxButtons.OK, MessageBoxIcon.Warning)
34             txtPassword.Focus()
35             Return
36         End If
37         If txtName.Text =
"" Then
38             MessageBox.Show(
"Please enter name", "", MessageBoxButtons.OK, MessageBoxIcon.Warning)
39             txtName.Focus()
40             Return
41         End If
42         If txtContactNo.Text =
"" Then
43             MessageBox.Show(
"Please enter contact no.", "", MessageBoxButtons.OK, MessageBoxIcon.Warning)
44             txtContactNo.Focus()
45             Return
46         End If
47         Try
48             con = New SqlConnection(cs)
49             con.Open()
50             Dim ct As String =
"select userid from registration where userid=@d1"
51             cmd = New SqlCommand(ct)
52             cmd.Parameters.AddWithValue(
"@d1", txtUserID.Text)
53             cmd.Connection = con
54             rdr = cmd.ExecuteReader()
55             If rdr.Read() Then
56                 MessageBox.Show(
"user id Already Exists", "Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
57                 txtUserID.Text =
""
58                 txtUserID.Focus()
59                 If (rdr IsNot Nothing) Then
60                     rdr.Close()
61                 End If
62                 Return
63             End If
64             con.Close()
65             If chkActive.Checked = True Then
66                 st1 =
"Yes"
67             Else
68                 st1 =
"No"
69             End If
70             con = New SqlConnection(cs)
71             con.Open()
72             Dim cb As String =
"insert into Registration(userid, UserType, Password, Name, ContactNo, EmailID,JoiningDate,Active) VALUES (@d1,@d2,@d3,@d4,@d5,@d6,@d7,@d8)"
73             cmd = New SqlCommand(cb)
74             cmd.Connection = con
75             cmd.Parameters.AddWithValue(
"@d1", txtUserID.Text)
76             cmd.Parameters.AddWithValue(
"@d2", cmbUserType.Text)
77             cmd.Parameters.AddWithValue(
"@d3", Encrypt(txtPassword.Text.Trim()))
78             cmd.Parameters.AddWithValue(
"@d4", txtName.Text)
79             cmd.Parameters.AddWithValue(
"@d5", txtContactNo.Text)
80             cmd.Parameters.AddWithValue(
"@d6", txtEmailID.Text)
81             cmd.Parameters.AddWithValue(
"@d7", Now)
82             cmd.Parameters.AddWithValue(
"@d8", st1)
83             cmd.ExecuteReader()
84             con.Close()
85             Dim st As String =
"added the new user '" & txtUserID.Text & "'"
86             LogFunc(lblUser.Text, st)
87             MessageBox.Show(
"Successfully Registered", "User", MessageBoxButtons.OK, MessageBoxIcon.Information)
88             btnSave.Enabled = False
89             Getdata()
90         Catch ex As Exception
91             MessageBox.Show(ex.Message,
"Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
92         End Try
93     End Sub
94
95     Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
96         Try
97             If MessageBox.Show(
"Do you really want to delete this record?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.Yes Then
98                 DeleteRecord()
99             End If
100         Catch ex As Exception
101             MessageBox.Show(ex.Message,
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
102         End Try
103     End Sub
104     Private Sub DeleteRecord()
105
106         Try
107             If txtUserID.Text =
"admin" Or txtUserID.Text = "Admin" Then
108                 MessageBox.Show(
"Admin account can not be deleted", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
109                 Exit Sub
110             End If
111             Dim RowsAffected As Integer =
0
112             con = New SqlConnection(cs)
113             con.Open()
114             Dim cq As String =
"delete from Registration where userid='" & txtUserID.Text & "'"
115             cmd = New SqlCommand(cq)
116             cmd.Connection = con
117             RowsAffected = cmd.ExecuteNonQuery()
118             If RowsAffected >
0 Then
119                 Dim st As String =
"deleted the user '" & txtUserID.Text & "'"
120                 LogFunc(lblUser.Text, st)
121                 MessageBox.Show(
"Successfully deleted", "Record", MessageBoxButtons.OK, MessageBoxIcon.Information)
122                 Getdata()
123                 Reset()
124             Else
125                 MessageBox.Show(
"No Record found", "Sorry", MessageBoxButtons.OK, MessageBoxIcon.Information)
126                 Reset()
127             End If
128             If con.State = ConnectionState.Open Then
129                 con.Close()
130
131             End If
132         Catch ex As Exception
133             MessageBox.Show(ex.Message,
"Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
134         End Try
135     End Sub
136
137     Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
138         Try
139             If txtUserID.Text =
"" Then
140                 MessageBox.Show(
"Please enter user id", "", MessageBoxButtons.OK, MessageBoxIcon.Warning)
141                 txtUserID.Focus()
142                 Return
143             End If
144             If cmbUserType.Text =
"" Then
145                 MessageBox.Show(
"Please select user type", "", MessageBoxButtons.OK, MessageBoxIcon.Warning)
146                 cmbUserType.Focus()
147                 Return
148             End If
149             If txtPassword.Text =
"" Then
150                 MessageBox.Show(
"Please enter password", "", MessageBoxButtons.OK, MessageBoxIcon.Warning)
151                 txtPassword.Focus()
152                 Return
153             End If
154             If txtName.Text =
"" Then
155                 MessageBox.Show(
"Please enter name", "", MessageBoxButtons.OK, MessageBoxIcon.Warning)
156                 txtName.Focus()
157                 Return
158             End If
159             If txtContactNo.Text =
"" Then
160                 MessageBox.Show(
"Please enter contact no.", "", MessageBoxButtons.OK, MessageBoxIcon.Warning)
161                 txtContactNo.Focus()
162                 Return
163             End If
164             If chkActive.Checked = True Then
165                 st1 =
"Yes"
166             Else
167                 st1 =
"No"
168             End If
169             con = New SqlConnection(cs)
170             con.Open()
171             Dim cb As String =
"update registration set userid=@d1, usertype=@d2,password=@d3,name=@d4,contactno=@d5,emailid=@d6,Active=@d8 where userid=@d7"
172             cmd = New SqlCommand(cb)
173             cmd.Connection = con
174             cmd.Parameters.AddWithValue(
"@d1", txtUserID.Text)
175             cmd.Parameters.AddWithValue(
"@d2", cmbUserType.Text)
176             cmd.Parameters.AddWithValue(
"@d3", Encrypt(txtPassword.Text.Trim()))
177             cmd.Parameters.AddWithValue(
"@d4", txtName.Text)
178             cmd.Parameters.AddWithValue(
"@d5", txtContactNo.Text)
179             cmd.Parameters.AddWithValue(
"@d6", txtEmailID.Text)
180             cmd.Parameters.AddWithValue(
"@d7", TextBox1.Text)
181             cmd.Parameters.AddWithValue(
"@d8", st1)
182             cmd.ExecuteReader()
183             con.Close()
184             Dim st As String =
"updated the user '" & txtUserID.Text & "' details"
185             LogFunc(lblUser.Text, st)
186             MessageBox.Show(
"Successfully updated", "User Info", MessageBoxButtons.OK, MessageBoxIcon.Information)
187             btnUpdate.Enabled = False
188             Getdata()
189         Catch ex As Exception
190             MessageBox.Show(ex.Message,
"Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
191         End Try
192     End Sub
193     Public Sub Getdata()
194         Try
195             con = New SqlConnection(cs)
196             con.Open()
197             cmd = New SqlCommand(
"SELECT RTRIM(userid), RTRIM(UserType), RTRIM(Password), RTRIM(Name), RTRIM(EmailID), RTRIM(ContactNo),RTRIM(Active),JoiningDate from Registration order by JoiningDate", con)
198             rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
199             dgw.Rows.Clear()
200             While (rdr.Read() = True)
201                 dgw.Rows.Add(rdr(
0), rdr(1), Decrypt(rdr(2)), rdr(3), rdr(4), rdr(5), rdr(6), rdr(7))
202             End While
203             con.Close()
204         Catch ex As Exception
205             MessageBox.Show(ex.Message,
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
206         End Try
207     End Sub
208     Private Sub btnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNew.Click
209         Reset()
210     End Sub
211
212
213     Private Sub dgw_RowPostPaint(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowPostPaintEventArgs) Handles dgw.RowPostPaint
214         Dim strRowNumber As String = (e.RowIndex +
1).ToString()
215         Dim size As SizeF = e.Graphics.MeasureString(strRowNumber, Me.Font)
216         If dgw.RowHeadersWidth < Convert.ToInt32((size.Width +
20)) Then
217             dgw.RowHeadersWidth = Convert.ToInt32((size.Width +
20))
218         End If
219         Dim b As Brush = SystemBrushes.ControlText
220         e.Graphics.DrawString(strRowNumber, Me.Font, b, e.RowBounds.Location.X +
15, e.RowBounds.Location.Y + ((e.RowBounds.Height - size.Height) / 2))
221
222     End Sub
223
224     Private Sub frmRegistration_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
225         Getdata()
226     End Sub
227
228     Private Sub dgw_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dgw.MouseClick
229         Try
230             If dgw.Rows.Count >
0 Then
231                 Dim dr As DataGridViewRow = dgw.SelectedRows(
0)
232                 txtUserID.Text = dr.Cells(
0).Value.ToString()
233                 TextBox1.Text = dr.Cells(
0).Value.ToString()
234                 cmbUserType.Text = dr.Cells(
1).Value.ToString()
235                 txtPassword.Text = dr.Cells(
2).Value.ToString()
236                 txtName.Text = dr.Cells(
3).Value.ToString()
237                 txtContactNo.Text = dr.Cells(
5).Value.ToString()
238                 txtEmailID.Text = dr.Cells(
4).Value.ToString()
239                 If dr.Cells(
6).Value.ToString() = "Yes" Then
240                     chkActive.Checked = True
241                 Else
242                     chkActive.Checked = False
243                 End If
244                 btnUpdate.Enabled = True
245                 btnDelete.Enabled = True
246                 btnSave.Enabled = False
247             End If
248         Catch ex As Exception
249             MessageBox.Show(ex.Message,
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
250         End Try
251     End Sub
252
253     Private Sub btnCheckAvailability_Click(sender As System.Object, e As System.EventArgs) Handles btnCheckAvailability.Click
254         If txtUserID.Text =
"" Then
255             MessageBox.Show(
"Please enter user id", "", MessageBoxButtons.OK, MessageBoxIcon.Warning)
256             txtUserID.Focus()
257             Return
258         End If
259         Try
260             con = New SqlConnection(cs)
261             con.Open()
262             Dim ct As String =
"select userid from registration where userid=@d1"
263             cmd = New SqlCommand(ct)
264             cmd.Parameters.AddWithValue(
"@d1", txtUserID.Text)
265             cmd.Connection = con
266             rdr = cmd.ExecuteReader()
267             If rdr.Read() Then
268                 MessageBox.Show(
"User ID not available", "", MessageBoxButtons.OK, MessageBoxIcon.Information)
269                 If (rdr IsNot Nothing) Then
270                     rdr.Close()
271                 End If
272                 Return
273             Else
274                 MessageBox.Show(
"User ID available", "", MessageBoxButtons.OK, MessageBoxIcon.Information)
275                 If (rdr IsNot Nothing) Then
276                     rdr.Close()
277                 End If
278                 Return
279             End If
280         Catch ex As Exception
281             MessageBox.Show(ex.Message,
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
282         End Try
283     End Sub
284 End Class


Gõ tìm kiếm nhanh...